/*
 * Copyright (C) 2010 Denis Mingulov.
 *
 * This file is part of QReader QtWebView test.
 *
 * Author: Denis Mingulov <denis@mingulov.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */

#include "mainmenu.h"
#include "ui_mainmenu.h"
#include <QResizeEvent>
#include <QFileDialog>
#include <QMessageBox>
#include <qreadersettings.h>
#include "aboutwindow.h"


MainMenu::MainMenu(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MainMenu),
    fileDialog(new QFileDialog(this))
{
    ui->setupUi(this);
    connect(ui->openBookButton, SIGNAL(clicked()), this, SLOT(onOpenBook()));
    connect(ui->aboutButton, SIGNAL(clicked()), this, SLOT(onAbout()));
    addAction(ui->actionOptions);
    addAction(ui->actionExit);
    if(!QReaderSettings::instance().isOnlineAvailable())
        ui->onlineButton->setVisible(false);
    if(!QReaderSettings::instance().isOnlineAvailable())
        ui->openLastButton->setVisible(false);
}

MainMenu::~MainMenu()
{
    delete ui;
    delete fileDialog;
}

void MainMenu::changeEvent(QEvent *e)
{
    QWidget::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void MainMenu::resizeEvent(QResizeEvent *event)
{
    const QSize newSize = event->size();
    if(ui && ui->verticalLayoutWidget)
    {
        ui->verticalLayoutWidget->resize(newSize);
        ui->verticalLayoutWidget->move(0,0);
    }
}

void MainMenu::onOpenBook()
{
    if(fileDialog)
    {
//        fileDialog->setReadOnly(true);
//        delete fileDialog;
//        fileDialog = new QFileDialog(this);
        fileDialog->getOpenFileName(this, tr("Open File"), "", tr("All files (*.*)"));
    }
//    QFileDialog::getOpenFileName(this, tr("Open File"), "", tr("All files (*.*)"));
}

void MainMenu::onAbout()
{
    QMessageBox::about(this, tr("QReader: About..."), tr("QReader 3.00 / 2010-03-05\nQReader is a freeware e-book reader for mobile devices.\nCopyright (c) 2004-2010 Denis Mingulov"));
}
